from django.template import loader, RequestContext
from django.template import Context
from django.http import HttpResponse, Http404
import datetime
from django.contrib.auth.decorators import login_required

def hello(request):
    return HttpResponse("Hello world")

def current_datetime(request):
    now = datetime.datetime.now()
    request.session['foo'] = 'bar'
    
    t = loader.get_template('current_datetime.html')
    html = t.render(Context({'current_date': now, 'authenticated': request.user.is_authenticated()}))
    return HttpResponse(html)
    
def custom_proc(request):
    "A context processor that provides 'app', 'user' and 'ip_address'."
    return {
        'app': 'My app',
        'user': request.user,
        'ip_address': request.META['REMOTE_ADDR']
    }    

@login_required
def profile_view(request):
    username = request.user.username
    t = loader.get_template('profile.html')
    c = RequestContext(request, {'message': 'I am the profile view.'},
            processors=[custom_proc])
    html = t.render(c)
    return HttpResponse(html)
    
    
